﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Configuration;
using System.Data;

using Think.Lenovo.Com.Common;
using Think.Lenovo.Com.Models;
using Think.Lenovo.Com.Models.Users;
using Think.Lenovo.Com.DataAccess;
using Think.Lenovo.Com.DataAccess.User;

using Think.Lenovo.Com.Controllers.Filter;
namespace Think.Lenovo.Com.Controllers
{
    public class UserController : Controller
    {
        //[PowerFilterAttribute(PowerCode = "")]
        public ActionResult ViewManageUser(int pageIndex, int pageSize)
        {
            ViewUserModel vum = (ViewUserModel)Session["User"];
            #region//Step 1.得到角色列表
            Roles roles = new Roles();
            LinkedList<RoleModel> rs = roles.GetRoles();
            List<SelectListItem> roleList = new List<SelectListItem>();
            SelectListItem first = new SelectListItem();
            first.Value = "";
            first.Text = "- 请选择 -";
            roleList.Add(first);
            foreach (RoleModel rm in rs)
            {
                SelectListItem item = new SelectListItem();
                item.Text = rm.Name;
                item.Value = rm.Id.ToString();
                roleList.Add(item);
            }
            ViewData["UserRole"] = roleList;
            #endregion

            Think.Lenovo.Com.DataAccess.User.ViewUser u = new ViewUser();
            #region//Step 2.得到用户列表的第一页
            if (vum.UserRoleId == 1)
            {
                LinkedList<ViewUserModel> list = u.GetViewUsers(pageIndex, pageSize);
                ViewData["list"] = list;
            }
            else
            {
                LinkedList<ViewUserModel> list = u.GetViewUsers(pageIndex, pageSize, vum.CompanyRegion);
                ViewData["list"] = list;
            }

            #endregion

            #region//Step 3. 得到分页页码
            int userCount = 0;//用户总数
            if (vum.UserRoleId == 1)
            {
                userCount = u.GetUserCount();
            }
            else
            {
                userCount = u.GetUserCount(vum.CompanyRegion);
            }
            LinkedList<PageNumberModel> pages = PageBuilder.LinkedPage(userCount, pageSize, pageIndex, 10, "/User/ViewManageUser/{pageIndex}/" + pageSize, "{pageIndex}");
            ViewData["pager"] = pages;
            #endregion

            return View("/Views/ViewManageUser.cshtml");
        }

        public ActionResult GetUser(int id)
        {
            User u = new User();
            UserModel model = u.GetUser(id);
            JsonResult jr = new JsonResult();
            jr.Data = model;
            return jr;
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="model">用户实体</param>
        public ActionResult AddUser(UserModel model)
        {
            JsonResult jr = new JsonResult();
            Dictionary<Object, Object> result = new Dictionary<object, object>();
            try
            {
                User u = new User();
                long id = u.AddUser(model);
                if (id > 0)
                {
                    result.Add("isSuccess", id);
                }
                else
                {
                    result.Add("isSuccess", "-101");
                }
            }
            catch (Exception e)
            {
                String logFolder = ConfigurationManager.AppSettings["LogRoot"];
                Log.RecordLog(logFolder, e);
            }
            jr.Data = result;
            return jr;
        }
        /// <summary>
        /// 编辑用户
        /// </summary>
        /// <param name="model">用户实体</param>
        /// <returns></returns>
        public ActionResult EditUser(UserModel model)
        {
            User u = new User();
            long affect = u.Update(model);
            JsonResult jr = new JsonResult();
            Dictionary<Object, Object> result = new Dictionary<object, object>();
            result.Add("isSuccess", affect);
            jr.Data = result;
            return jr;
        }

        /// <summary>
        /// 根据角色Id得到该角色下的所有等级
        /// </summary>
        public ActionResult GetRoleLevels()
        {
            int roleId = 0;
            LinkedList<String> errors = new LinkedList<string>();
            LinkedList<UserLevelModel> levels = new LinkedList<UserLevelModel>();
            String str = Request["roleId"];
            if (!Int32.TryParse(str, out roleId))
            {
                errors.AddLast("请选择角色");
            }
            else if (roleId > 0)
            {
                RoleUserLevelRelation r = new RoleUserLevelRelation();
                LinkedList<RoleUserLevelRelationModel> list = r.GetLeveles(roleId);

                List<int> levelIds = new List<int>();
                foreach (RoleUserLevelRelationModel model in list)
                {
                    levelIds.Add(model.UserLevelId);
                }

                UserLevel ul = new UserLevel();
                levels = ul.GetUserLevels(levelIds);
            }
            else
            {
                errors.AddLast("角色id小于等于零");
            }
            Dictionary<Object, Object> result = new Dictionary<object, object>();
            result.Add("errors", errors);
            result.Add("DataList", levels);
            JsonResult jr = new JsonResult();
            jr.Data = result;
            return jr;
        }
        /// <summary>
        /// 设置用户状态
        /// </summary>
        /// <param name="model">用户实体</param>
        /// <returns></returns>
        public ActionResult SetState(UserModel model)
        {
            User u = new User();
            int state = model.UserStateId;
            int userId = model.Id;
            long affect = u.Update(userId, state);
            JsonResult jr = new JsonResult();
            Dictionary<Object, Object> result = new Dictionary<object, object>();
            result.Add("isSuccess", affect);
            jr.Data = result;
            return jr;
        }
        /// <summary>
        /// 根据角色id得到该角色的所有权限 
        /// </summary>
        /// <param name="id">角色id</param>
        /// <returns>角色和权限的关系实体集合</returns>
        public ActionResult ViewGrantPowerToUser(int id)
        {
            Power p = new Power();
            LinkedList<PowerModel> list = new LinkedList<PowerModel>();
            list = p.GetAllPower();
            ViewData["list"] = list;
            ViewData["roleId"] = id;//角色id
            #region//根据角色id得到角色名称
            Roles rs = new Roles();
            RoleModel rm = rs.GetRole(id);
            ViewData["RoleName"] = rm.Name;
            #endregion
            #region //根据角色id,得到该角色的所有权限
            RolePowerRelation rpr = new RolePowerRelation();
            LinkedList<RolePowerRelationModel> rolePowers = new LinkedList<RolePowerRelationModel>();
            rolePowers = rpr.GetRolePowerRelationModels(id);
            ViewData["rolePowers"] = rolePowers;
            #endregion
            return View("/Views/ViewGrantPower.cshtml");
        }
        /// <summary>
        /// 将指定权限授予指定用户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GrantPowerToUser()
        {
            LinkedList<String> errors = new LinkedList<String>();
            String strRoleId = Request["roleId"];
            String strPowerIds = Request["powerIds"];
            int roleId = 0;
            List<int> powerIds = new List<int>();
            int i = 0;
            #region//验证客户端传过来的数据
            if (!Int32.TryParse(strRoleId, out roleId))
            {
                errors.AddLast("角色id不是数字");
            }
            else if (roleId < 1)
            {
                errors.AddLast("角色id小于1");
            }
            if (String.IsNullOrEmpty(strPowerIds))
            {
                errors.AddLast("没有选择权限");
            }
            else
            {
                String[] ss = strPowerIds.Split(',');
                foreach (String s in ss)
                {
                    if (!Int32.TryParse(s, out i))
                    {
                        errors.AddLast("权限id不是整数");
                    }
                    else if (i < 1)
                    {
                        errors.AddLast("权限id小于1");
                    }
                    else
                    {
                        powerIds.Add(i);
                    }
                }
            }
            #endregion
            if (errors.Count < 1)
            {
                RolePowerRelation rpr = new RolePowerRelation();
                //step 1. 删除该角色的所有权限
                rpr.DisconnectRelaction(roleId);
                //step 2. 授权
                rpr.AddRolePowerRelation(roleId, powerIds);
            }
            Dictionary<Object, Object> result = new Dictionary<object, object>();
            result.Add("errors", errors);
            JsonResult jr = new JsonResult();
            jr.Data = result;
            return jr;
        }

        /// <summary>
        /// 显示修改用户信息的页面 
        /// </summary>
        /// <param name="id">用户id</param>
        /// <returns></returns>
        public ActionResult ViewEditUserInfo(int id)
        {
            try
            {
                UserInfo ui = new UserInfo();
                UserInfoModel model = ui.GetUserInfo(id);
                #region//step 1.得到一个用户详细信息对象 
                if (model.UserId == 0)
                {
                    model.UserId = id;
                }
                ViewBag.UserInfo = model;
                #endregion

                #region//step 2.公司类型

                CompanyType ct = new CompanyType();
                LinkedList<CompanyTypeModel> comTypes = ct.GetCompanyTypeModels();
                List<SelectListItem> comTypeList = new List<SelectListItem>();
                SelectListItem first = new SelectListItem();
                first.Value = "";
                first.Text = "- 请选择 -";
                comTypeList.Add(first);
                foreach (CompanyTypeModel m in comTypes)
                {
                    SelectListItem sli = new SelectListItem();
                    if (m.Id == model.CompanyType)
                    {
                        sli.Selected = true;
                    }
                    sli.Text = m.CompanyTypeName;
                    sli.Value = m.Id.ToString();
                    comTypeList.Add(sli);
                }
                ViewBag.CompanyType = comTypeList;
                #endregion

                #region//step 3.公司性质

                CompanyProperty cp = new CompanyProperty();
                LinkedList<CompanyPropertyModel> comPropertyModels = cp.GetPropertyModels();
                List<SelectListItem> comPropertyList = new List<SelectListItem>();
                first = new SelectListItem();
                first.Value = "";
                first.Text = "- 请选择 -";
                comPropertyList.Add(first);
                foreach (CompanyPropertyModel m in comPropertyModels)
                {
                    SelectListItem sli = new SelectListItem();
                    if (m.Id == model.CompanyProperty)
                    {
                        sli.Selected = true;
                    }
                    sli.Text = m.CompanyProperty;
                    sli.Value = m.Id.ToString();
                    comPropertyList.Add(sli);
                }
                ViewBag.CompanyProperty = comPropertyList;
                #endregion

                #region 4. 得到用户所属省市
                RegionInfo regionInfo = new RegionInfo();
                //ViewBag.provinceId = model.CompanyProvinceId;
                LinkedList<RegionModel> provinces = regionInfo.GetAllProvince();
                //ViewBag.cityId = model.CompanyCityId;
                LinkedList<RegionModel> cities = regionInfo.GetCitiesByProvince(model.CompanyProvinceId);

                #region 4.1 省
                 
                List<SelectListItem> provinceList = new List<SelectListItem>();
                first = new SelectListItem();
                first.Value = "";
                first.Text = "- 请选择省份 -";
                provinceList.Add(first);
                foreach (RegionModel m in provinces)
                {
                    SelectListItem sli = new SelectListItem();
                    if (m.Id == model.CompanyProvinceId)
                    {
                        sli.Selected = true;
                    }
                    sli.Text = m.NodeName;
                    sli.Value = m.Id.ToString();
                    provinceList.Add(sli);
                }
                ViewBag.CompanyProvinceId = provinceList;
                #endregion

                #region 4.2城市
                List<SelectListItem> cityList = new List<SelectListItem>();
                first = new SelectListItem();
                first.Value = "";
                first.Text = "- 请选择城市 -";
                cityList.Add(first);
                foreach (RegionModel m in cities)
                {
                    SelectListItem sli = new SelectListItem();
                    if (m.Id == model.CompanyCityId)
                    {
                        sli.Selected = true;
                    }
                    sli.Text = m.NodeName;
                    sli.Value = m.Id.ToString();
                    cityList.Add(sli);
                }
                ViewBag.CompanyCityId = cityList;
                #endregion

                #endregion
            }
            catch (Exception e)
            {
                String logFolder = ConfigurationManager.AppSettings["LogRoot"];
                Log.RecordLog(logFolder, e);
            }
            return View("/Views/ViewEditUserInfo.cshtml");
        }

        /// <summary>
        ///渠道商添加用户信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public void SaveUserInfo(UserInfoModel model)
        {
            try
            {
                //分销商添加经销商,给此分销商 +100 分
                ViewUserModel user = (ViewUserModel)Session["User"];
                UserInfo ui = new UserInfo();
                UserInfoModel m = ui.GetUserInfo(model.UserId);
                if (m != null && m.UserId > 0)
                {
                    RegionInfo regionInfo = new RegionInfo();
                    RegionModel bigArea = regionInfo.GetBigAreaByProvinceId(model.CompanyProvinceId);
                    model.CompanyRegion = bigArea.Id;
                    ui.UpdateUserInfo(model);
                }
                else
                {
                    RegionInfo regionInfo = new RegionInfo();
                    RegionModel bigArea = regionInfo.GetBigAreaByProvinceId(model.CompanyProvinceId);
                    model.CompanyRegion = bigArea.Id;
                    ui.AddUserInfo(model);
                    //判断用户是否是渠道商,如果是,给该渠道商加100分
                    ui.AddIntegral(user.Id, 100);
                }
                Response.Write("<script type=\"text/javascript\">");
                Response.Write("alert(\"完善用户资料成功\");");
                Response.Write("window.location='/User/ViewManageUser/1/10'");
                Response.Write("</script>");
            }
            catch (Exception e)
            {
                String logFolder = ConfigurationManager.AppSettings["LogRoot"];
                e.Source += "添加用户信息时发生错误!";
                Log.RecordLog(logFolder, e);
                Response.Write("<script type=\"text/javascript\">");
                Response.Write("alert(\"完善用户资料失败\");");
                Response.Write("windows.location.href='/User/ViewManageUser/1/10'");
                Response.Write("</script>");
            }
        }

        /// <summary>
        /// 注册页面
        /// </summary>
        /// <returns></returns>
        public ActionResult ViewRegisterUser()
        {
            #region//step 1.公司类型

            CompanyType ct = new CompanyType();
            LinkedList<CompanyTypeModel> comTypes = ct.GetCompanyTypeModels();
            List<SelectListItem> comTypeList = new List<SelectListItem>();
            SelectListItem first = new SelectListItem();
            first.Value = "";
            first.Text = "- 请选择 -";
            comTypeList.Add(first);
            foreach (CompanyTypeModel m in comTypes)
            {
                SelectListItem sli = new SelectListItem();
                sli.Text = m.CompanyTypeName;
                sli.Value = m.Id.ToString();
                comTypeList.Add(sli);
            }
            ViewBag.CompanyType = comTypeList;
            #endregion

            #region//step 2.公司性质

            CompanyProperty cp = new CompanyProperty();
            LinkedList<CompanyPropertyModel> comPropertyModels = cp.GetPropertyModels();
            List<SelectListItem> comPropertyList = new List<SelectListItem>();
            first = new SelectListItem();
            first.Value = "";
            first.Text = "- 请选择 -";
            comPropertyList.Add(first);
            foreach (CompanyPropertyModel m in comPropertyModels)
            {
                SelectListItem sli = new SelectListItem();
                sli.Text = m.CompanyProperty;
                sli.Value = m.Id.ToString();
                comPropertyList.Add(sli);
            }
            ViewBag.CompanyProperty = comPropertyList;
            #endregion

            return View("/Views/ViewRegisterUser.cshtml");
        }

        public void RegisterUser(ViewUserModel model)
        {
            LinkedList<String> errors = new LinkedList<string>();
            #region//验证
            #region//用户名
            if (String.IsNullOrEmpty(model.UserName) || model.UserName.Trim().Length == 0)
            {
                errors.AddLast("用户名不能为空");
            }
            else
            {
                User u = new User();
                model.UserName = model.UserName.Trim();
                ViewUserModel umodel = u.GetUser(model.UserName);
                if (umodel != null && umodel.Id > 0)
                {
                    errors.AddLast("用户名已存在!");
                }
            }
            #endregion
            #region//密码
            if (String.IsNullOrEmpty(model.UserPassword))
            {
                errors.AddLast("密码不能为空");
            }
            #endregion
            #region//公司名称
            if (String.IsNullOrEmpty(model.CompanyName) || model.CompanyName.Trim().Length == 0)
            {
                errors.AddLast("公司名称不能为空!");
            }
            else
            {
                model.CompanyName = model.CompanyName.Trim();
            }
            #endregion
            #region//联系人真实姓名
            if (String.IsNullOrEmpty(model.RealName) || model.RealName.Trim().Length == 0)
            {
                errors.AddLast("联系人真实姓名不能为空");
            }
            else
            {
                model.RealName = model.RealName.Trim();
            }
            #endregion
            #region//公司地址
            if (String.IsNullOrEmpty(model.CompanyAddress) || model.CompanyAddress.Trim().Length == 0)
            {
                errors.AddLast("公司地址不能为空");
            }
            else
            {
                model.CompanyAddress = model.CompanyAddress.Trim();
            }
            #endregion
            #region//公司Email
            if (String.IsNullOrEmpty(model.CompanyEmail) || model.CompanyEmail.Trim().Length == 0)
            {
                errors.AddLast("公司Email不能为空");
            }
            else
            {
                model.CompanyEmail = model.CompanyEmail.Trim();
            }
            #endregion
            #region//公司电话
            if (String.IsNullOrEmpty(model.CompanyTel) || model.CompanyTel.Trim().Length == 0)
            {
                errors.AddLast("公司电话不能为空");
            }
            else
            {
                model.CompanyTel = model.CompanyTel.Trim();
            }
            #endregion
            #region//联系人电话
            if (String.IsNullOrEmpty(model.UserPhoneNumber) || model.UserPhoneNumber.Trim().Length == 0)
            {
                errors.AddLast("联系人电话不能为空");
            }
            else
            {
                model.UserPhoneNumber = model.UserPhoneNumber.Trim();
            }
            #endregion
            #region//注册日期
            {
                DateTime dt = DateTime.Now;
                if (!DateTime.TryParse(model.CompanyRegisteDate.ToString(), out dt))
                {
                    errors.AddLast("公司注册日期不是一个日期格式");
                }
            }
            #endregion
            #region//公司类型
            {
                int i;
                if (!Int32.TryParse(model.CompanyTypeId.ToString(), out i))
                {
                    errors.AddLast("必须选择公司类型");
                }
                else if (i < 1)
                {
                    errors.AddLast("必须选择公司类型");
                }
            }
            #endregion
            #endregion
            if (errors.Count > 0)
            {
                System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                String s = serializer.Serialize(errors);
                Response.Write("<script type='text/javascript'>alert('" + s + "');</script>");
                return;
            }
            else
            {
                try
                {
                    bool success = false;
                    #region//step 1. 插入用户表
                    UserModel umodel = new UserModel();
                    umodel.UserName = model.UserName;
                    umodel.UserPassword = model.UserPassword;
                    umodel.UserRoleId = 4;
                    umodel.UserLevelId = 4;
                    User u = new User();
                    long id = u.AddUser(umodel);
                    if (id < 1)
                    {
                        success = false;
                    }
                    else
                    {
                        #region//step 2.将用户信息插入用户信息表
                        UserInfoModel infomodel = new UserInfoModel();
                        infomodel.CompanyAddress = model.CompanyAddress;
                        infomodel.CompanyBusinessNumber = model.CompanyBusinessNumber;
                        infomodel.CompanyEmail = model.CompanyEmail;
                        infomodel.CompanyFax = model.CompanyFax;
                        infomodel.CompanyGrade = 50;
                        infomodel.CompanyName = model.CompanyName;
                        infomodel.CompanyPostalCode = model.CompanyPostalCode;
                        infomodel.CompanyProperty = model.CompanyPropertyId;
                        infomodel.CompanyRegion = model.CompanyRegion;
                        infomodel.CompanyRegisteDate = model.CompanyRegisteDate;
                        infomodel.CompanyRegisteFund = model.CompanyRegisteFund;
                        infomodel.CompanyTel = model.CompanyTel;
                        infomodel.CompanyType = model.CompanyTypeId;
                        infomodel.FormatedUserName = model.FormatedUserName;
                        infomodel.Forward = model.CompanyForward;
                        infomodel.RealName = model.RealName;
                        infomodel.UserEmail = model.UserEmail;
                        infomodel.UserId = (int)id;
                        infomodel.UserPhoneNumber = model.UserPhoneNumber;
                        UserInfo ui = new UserInfo();
                        id = ui.AddUserInfo(infomodel);
                        if (id > 0)
                        {
                            success = true;
                        }
                        else
                        {
                            success = false;
                        }
                        #endregion
                    }
                    #endregion
                    if (success)
                    {
                        Response.Write("<script type='text/javascript'>alert('注册成功');</script>");
                        return;
                    }
                    else
                    {
                        Response.Write("<script type='text/javascript'>alert('注册失败');</script>");
                        return;
                    }
                }
                catch (Exception e)
                {
                    String logFolder = ConfigurationManager.AppSettings["LogRoot"];
                    e.Source += "注册用户时,发生了异常";
                    Log.RecordLog(logFolder, e);
                    Response.Write("<script type='text/javascript'>alert('注册失败,发生异常,请查看日志!');</script>");
                }
            }
        }

        [HttpGet]
        public void OperateUserState()
        {
            Dictionary<String, Object> result = new Dictionary<string, object>();
            List<String> errors = new List<string>();
            String stateStr = Request["stateId"];
            int stateId = 0;

            String ids = Request["ids"];
            #region//验证
            if (String.IsNullOrEmpty(stateStr))
            {
                errors.Add("状态id为空");
            }
            else if (!Int32.TryParse(stateStr, out stateId))
            {
                errors.Add("用户状态码必须为数字");
            }
            else if (stateId < 1 || stateId > 3)
            {
                errors.Add("用户状态码不合法");
            }
            if (String.IsNullOrEmpty(ids))
            {
                errors.Add("请选择用户");
            }
            #endregion
            if (errors.Count < 1)
            {
                #region//将用户id转为int
                String[] ss = ids.Split(',');
                List<int> lst = new List<int>();
                int i = 0;
                foreach (String s in ss)
                {
                    if (Int32.TryParse(s, out i))
                    {
                        lst.Add(i);
                    }
                }
                #endregion
                if (lst.Count > 0)
                {
                    User u = new User();
                    u.Update(lst.ToArray(), stateId);
                }
                else
                {
                    errors.Add("请选择用户id");
                }
            }

            result.Add("errors", errors);
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            String json = serializer.Serialize(result);
            Response.Write(json);
        }
        [HttpGet]
        public ActionResult ApplyRole(int id)
        {
            JsonResult jr = new JsonResult();
            jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            Dictionary<String, Object> result = new Dictionary<string, object>();
            List<String> errors = new List<string>();
            ApplyRoleModel model = new ApplyRoleModel();
            model.IsPass = false; 
            model.UserId = id;
            model.ApplyDateTime = DateTime.Now;
            model.Message = "";
            #region//验证
            if (id < 1)
            {
                errors.Add("您未登录,请登录!");
            }
            #endregion

            if (errors.Count < 1)
            {
                model.ApplyDateTime = DateTime.Now;
                model.RoleId = 3;//用户要成为角色为3的用户
                ApplyRole applyRole = new ApplyRole();
                try
                {
                    int i = applyRole.Add(model);
                    if(i<1)
                    {
                        errors.Add("申请成功!");
                    }
                }
                catch (Exception e)
                {
                    String LogFolder = ConfigurationManager.AppSettings["LogRoot"];
                    Log.RecordLog(LogFolder, e);
                    errors.Add("申请失败 x0002s");
                }
            }
            result.Add("errors",errors);
            jr.Data = result;
            return jr;
        }

        /// <summary>
        /// 审核未在册经销营商成为在册经销商
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Audit()
        {
            JsonResult jr = new JsonResult();
            jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            List<String> errors = new List<string>();
            int Id = 0;
            int StateId = 0;
            try
            {
                if (Int32.TryParse(Request["Id"], out Id) && Int32.TryParse(Request["stateId"], out StateId))
                {
                    ApplyRole apply = new ApplyRole();
                    if (Id > 0)
                    {
                        ApplyRoleModel model = apply.GetModel(Id);
                        Think.Lenovo.Com.DataAccess.User.User u = new User();
                        UserModel umodel = u.GetUser(model.UserId);
                        if (StateId > 0)
                        {
                            apply.AuditApply(Id, true);
                            umodel.UserRoleId = 3;
                            u.Update(umodel,false);
                        }
                        else
                        {
                            apply.AuditApply(Id, false);
                            umodel.UserRoleId = 4;
                            u.Update(umodel, false);
                        }
                    }
                    else
                    {
                        errors.Add("Id错误");
                    }
                }
                else
                {
                    errors.Add("参数错误");
                }
            }
            catch (Exception e)
            {
                String logFolder = ConfigurationManager.AppSettings["LogRoot"];
                Log.RecordLog(logFolder, e);
                errors.Add("审核失败");
            }

            Dictionary<String, Object> result = new Dictionary<string, object>();
            result.Add("errors", errors);
            jr.Data = result;
            return jr;
        }

        public ActionResult ViewUserApply(int pageIndex, int pageSize)
        {
            ApplyRole apply = new ApplyRole();
            DataSet list = apply.GetPageData(pageIndex, pageSize, "", " applyDateTime ");
            ViewBag.List = list;

            int recordCount = apply.GetRecordCount("");
            LinkedList<PageNumberModel> pages = PageBuilder.LinkedPage(recordCount, pageSize, pageIndex, 10, "/User/ViewUserApply/{pageIndex}/" + pageSize, "{pageIndex}");
            ViewBag.Pager = pages;
            return View("/Views/ViewUserApply.cshtml") ;
        }
    }
}
